/*
 * Copyright 2018 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.utils;

import org.apache.shiro.SecurityUtils;

import com.lap.scd.commons.domain.BaseDO;
import com.lap.scp.admin.domain.UserDO;

/**
 * <pre>
 * 用户信息处理工具类
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018年2月18日 下午7:03:34
 * @since 1.0
 */
public abstract class UserUtils {

	/**
	 * 对创建人、修改人进行赋值
	 * 
	 * @param baseDO
	 */
	public static void setUsername(BaseDO baseDO) {
		UserDO userDO = UserUtils.getUser();
		if (baseDO != null && userDO != null) {
			baseDO.setCreatedBy(userDO.getUsername());
			baseDO.setUpdatedBy(userDO.getUsername());
		}
	}

	/**
	 * <pre>
	 * 获取当前登录的用户
	 * </pre>
	 * 
	 * @return
	 */
	public static UserDO getUser() {
		UserDO userDO = null;
		try {
			userDO = (UserDO) SecurityUtils.getSubject().getPrincipal();
		} catch (Exception e) {
		}
		return userDO;
	}

	/**
	 * 获取用户名
	 * 
	 * @return
	 */
	public static String get() {
		UserDO userDO = UserUtils.getUser();
		if (userDO != null) {
			return userDO.getUsername();
		} else {
			return "--";
		}
	}

	/**
	 * 获取用户ID
	 * 
	 * @return
	 */
	public static int getId() {
		UserDO userDO = UserUtils.getUser();
		if (userDO != null) {
			return userDO.getId();
		} else {
			return 0;
		}
	}

}
